package com.loanbefore.watermark;
 
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017-2-20.
 */

public class WaterMarkMoudel extends ReactContextBaseJavaModule {

    private String sysImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/afterLoan/images";
    // 四个需要拍照的位置的照片名称
    private String photoName = "";
    // 水印文字
    private String waterWord = "";
    private Bitmap photo = null;
    public WaterMarkMoudel(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "WaterMark";
    }
    @ReactMethod
    public void setWaterMark(String path, Callback success){
        //path = getRealPathFromUri(this.getCurrentActivity(),Uri.parse(path));
        File file = null;
        Uri imageUri = null;
        photoName = "";
        path = path.replace("file:///","/");
        file = new File(path);
//        if(!file.exists()){
//            file.mkdirs();
//        }
        imageUri = Uri.fromFile(file);
        if (imageUri != null) {
            //BaseFragment baseFragment = ((BaseFragment) getFragmentManager().findFragmentByTag("DH"));
            SimpleDateFormat datefromat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //waterWord = waterWord + "客户位置：" + longitude + " , " + latitude + "\n\r";
            waterWord = waterWord + "拍摄时间：" + datefromat.format(new Date());
            // 压缩照片
            //CompressionBitmap.saveReducePic(file.getAbsolutePath(), path, 100);
            // 添加水印文字
            waterMarkPhoto(file, waterWord);
            success.invoke(imageUri.toString());
            // 将图片转换成base64
            //base64 = ImgByteUtil.imgToBase64(file);
            // 将图片转换成的BASE64加密保存到TXT文件中
            //encryptPhoto(base64, file.getAbsolutePath().replace(".jpg", ".txt"));
            // 发送系统内置的广播去刷新相册实现显示
            //reFreshPhoto(file);
            // 回调JS函数，进行图片显示
            //baseFragment.getWebView().loadUrl("javascript:takePhotoSuccess('"+ photoName +"||||" + base64 + "')");
        }
    }
    public void waterMarkPhoto(File file, String word)
    {
        Uri imageUri = Uri.fromFile(file);
        Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath());
        photo = PictureAddWords.watermarkBitmap(bitmap, null, word);
        try
        {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            photo.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
        }
        catch (IOException e)
        {
            //LogUtil.e(TAG, e.getMessage());
        }
        photo = null;
    }
    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}
